import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_advanced_seekbar/flutter_advanced_seekbar.dart';
import 'package:image_picker/image_picker.dart';
import 'package:permission_handler/permission_handler.dart';

class LoveWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _LoveState();
  }
}

class _LoveState extends State<LoveWidget> {
  PickedFile? _imgPath; //图片路径
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Container(
          color: Colors.black,
          child: FractionallySizedBox(
            alignment: Alignment.center,
            widthFactor: 1,
            heightFactor: 0.5,
            child: _ImageView(_imgPath!),
          ),
        ),

        ///影片选择
        Positioned(
            left: 20, right: 20, top: 480, bottom: 80, child: _bottomWidget()),

        ///拍摄，选择图片按钮
        Positioned(
            left: 0,
            right: 0,
            top: 560,
            bottom: 0,
            child: _bottomContainer(context))
      ],
    );
  }

//图片显示
  Widget _ImageView(PickedFile imgPath) {
    if (imgPath == null) {
      return Center(
        child: Text(
          "请选择图片或拍照",
          style: TextStyle(color: Colors.white),
        ),
      );
    } else {
      print({"imgPath is $imgPath"});
      return Image.file(
        File(imgPath!.path),
        fit: BoxFit.cover,
      );
    }
  }

  ///底部控件
  _bottomWidget() {
    return AdvancedSeekBar(
      Colors.red,
      10,
      Colors.blue,
      lineHeight: 5,
      defaultProgress: 50,
      scaleWhileDrag: true,
      percentSplit: 10,
      percentSplitColor: Colors.green,
      percentSplitWidth: 1,
      seekBarStarted: () {},
      seekBarProgress: (v) {},
      seekBarFinished: (v) {},
    );
  }

  ///底部控件
  _bottomContainer(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          _buildWidget(context, Icons.photo_camera, "拍照"),
          _buildWidget(context, Icons.photo, "选择照片"),
        ],
      ),
    );
  }

//自定义控件
  _buildWidget(BuildContext context, IconData icon, String text) {
    return Container(
      alignment: Alignment.center,
      width: MediaQuery.of(context).size.width / 2,
      child: GestureDetector(
        child: Column(
          children: [
            Icon(
              icon,
              size: 30,
              color: Colors.white,
            ),
            Text(
              text,
              style: TextStyle(color: Colors.white),
            ),
          ],
        ),
        onTap: () {
          _buildOnTap(text);
        },
      ),
    );
  }

  ///点击事件
  void _buildOnTap(String text) {
    switch (text) {
      case "拍照":
        _takePhoto();
        break;
      case "选择照片":
        _openGallery();
        break;
    }
  }

  ///拍照
  void _takePhoto() async {
    //检查是否已有读写内存权限
    var permissionStatus = await Permission.camera.request();
    if (permissionStatus == PermissionStatus.granted) {
      var image =
          // ignore: invalid_use_of_visible_for_testing_member
          await ImagePicker.platform.pickImage(source: ImageSource.camera);
      setState(() {
        _imgPath = image;
      });
    }
  }

  ///相册
  _openGallery() async {
    var permissionStatus = await Permission.camera.request();
    if (permissionStatus == PermissionStatus.granted) {
      var image =
          // ignore: invalid_use_of_visible_for_testing_member
          await ImagePicker.platform.pickImage(source: ImageSource.gallery);
      setState(() {
        print({"imgPath is $image"});
        _imgPath = image;
      });
    }
  }
}
